#include<linux/module.h>
#include<linux/sched.h>
#include<linux/pid.h>
#include<linux/wait.h>
#include<linux/completion.h>
#include<linux/kthread.h>
MODULE_LICENSE("GPL");

static struct completion comple;
struct task_struct *old_thread;

int my_function(void *argc)
{
	wait_queue_head_t head;
	wait_queue_t data;
	printk("in the kernel thread function!\n");
	init_waitqueue_head(&head);
	init_waitqueue_entry(&data,current);
	add_wait_queue(&head,&data);
	schedule_timeout_uninterruptible(10);
	printk("the current pid is:%d\n",current->pid);
	printk("the state of the parent is:%ld\n",old_thread->state);
	complete(&comple);
	printk("out the kernel thread function\n");
	return 0;
}
static int __init wait_for_completion_init(void)
{
	struct task_struct *result;
	wait_queue_t data;
	printk("into wait_for_completion_init.\n");
	old_thread=current;
	result=kthread_create_on_node(my_function,NULL,-1,"wait_for_completion");

	wake_up_process(result);

	init_completion(&comple);
	init_waitqueue_entry(&data,result);
	__add_wait_queue_tail(&(comple.wait),&data);
	wait_for_completion(&comple);
	printk("the pid of new thread is:%d\n",result->pid);
	printk("the current pid is:%d\n",current->pid);
	printk("out wait_for_completion_init.\n");
	return 0;
}
static void __exit wait_for_completion_exit(void)
{
	printk("Goodbye wait_for_completion!\n");
}
module_init(wait_for_completion_init);
module_exit(wait_for_completion_exit);
